/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.internal.core.ioc;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IExecutableExtensionFactory;
import org.eclipse.core.runtime.IExtension;

import com.googlecode.tapestry.ide.TapestryPlugin;
import com.googlecode.tapestry.ide.core.ioc.IBeanFactory;

/**
 * A factory for creating BeanFactoryExtension objects.
 */
public class BeanFactoryExtensionFactory implements
		IExecutableExtensionFactory, IExecutableExtension {

	/** The bean. */
	private Object bean;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.IExecutableExtensionFactory#create()
	 */
	public Object create() throws CoreException {
		return bean;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org
	 * .eclipse.core.runtime.IConfigurationElement, java.lang.String,
	 * java.lang.Object)
	 */
	public void setInitializationData(IConfigurationElement config,
			String propertyName, Object data) throws CoreException {
		String beanName = getBeanName(data, config);
		IBeanFactory appContext = TapestryPlugin.getDefault().getBeanFactory();

		if (appContext == null) {
			throw new RuntimeException("Application context not found.");
		}

		if (beanName == null) {
			throw new RuntimeException("Bean id could not be identified.");
		}

		this.bean = appContext.getBean(beanName);

		if (this.bean instanceof IExecutableExtension) {
			((IExecutableExtension) this.bean).setInitializationData(config,
					propertyName, data);
		}
	}

	/**
	 * Gets the bean name.
	 * 
	 * @param data
	 *            the data
	 * @param config
	 *            the config
	 * 
	 * @return the bean name
	 */
	private String getBeanName(Object data, IConfigurationElement config) {

		// try the specific bean id the extension defines
		if (data != null && data.toString().length() > 0) {
			return data.toString();
		}

		// try the id of the config element
		if (config.getAttribute("id") != null) {
			return config.getAttribute("id");
		}

		// try the id of the extension element itself
		if (config.getParent() != null
				&& config.getParent() instanceof IExtension) {
			IExtension extensionDefinition = (IExtension) config.getParent();
			return extensionDefinition.getSimpleIdentifier();
		}

		return null;
	}

}
